# here put the import lib
# @Time : 2020/8/30 18:39
# @Author : liruiyang
# @File : loginResource
# @Software: PyCharm
from flask import jsonify, g
from flask_login import login_user, logout_user
from flask_restplus import Resource
from werkzeug.security import check_password_hash

from ..models.UserMinModel import UserMin
from ..service.userInfoManager import UserInfoManager
from ..utils import commons
from ..utils.response_code import RET
from . import main_rest_api, back_model

user_post_parser = main_rest_api.parser()
user_post_parser.add_argument('user_name', type=str, location='form')
user_post_parser.add_argument('password', type=str, location='form')
user_post_parser.add_argument('user_type', type=str, location='form')
user_post_parser.add_argument('other_info', type=str, location='form')
user_post_parser.add_argument('mail', type=str, location='form', required=True)

user_put_parser = main_rest_api.parser()
user_put_parser.add_argument('old_password', type=str, location='form', required=True)
user_put_parser.add_argument('new_password', type=str, location='form', required=True)


@main_rest_api.response(400, "参数不正确")
class UserResource(Resource):
    @main_rest_api.doc(description="注册账号")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(user_post_parser)
    def post(self):
        kwargs = user_post_parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        result_dict = UserInfoManager.add(**kwargs)
        return result_dict

    @main_rest_api.doc(description="修改账号密码")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(user_put_parser)
    def put(self):
        kwargs = user_put_parser.parse_args()

        # 检查密码
        args = {
            "user_id": g.user.user_id,
        }

        old_password = kwargs.get('old_password')

        result = UserInfoManager.get(**args)

        if result.get("code") != RET.OK:
            return result

        if not check_password_hash(result.get("data").get("password"), old_password):
            return {"code": RET.PWDERR, "msg": '用户名或密码错误'}

        args = {
            "user_id": g.user.user_id,
            "password": kwargs.get("new_password")
        }

        reset_dict = UserInfoManager.reset_password(**args)
        if reset_dict.get("code") != "200":
            return reset_dict

        logout_user()
        return {"code": RET.OK, "msg": "重置密码成功"}


userLogin_post_parser = main_rest_api.parser()
userLogin_post_parser.add_argument('mail', type=str, location='form', required=True)
userLogin_post_parser.add_argument('password', type=str, location='form', required=True)


@main_rest_api.response(400, "参数不正确")
class UserLoginResource(Resource):
    @main_rest_api.doc(description="登录账号")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(userLogin_post_parser)
    def post(self):
        kwargs = userLogin_post_parser.parse_args()
        args = {
            "mail": kwargs.get("mail"),
        }

        old_password = kwargs.get('password')

        result_dict = UserInfoManager.get(**args)

        if result_dict.get("code") != RET.OK:
            return result_dict

        if not check_password_hash(result_dict.get("data").get("password"), old_password):
            return {"code": RET.PWDERR, "msg": '用户名或密码错误'}

        if result_dict.get("code") == "200":
            user_dict = UserMin.get(result_dict['data'].get("userID"))
            if user_dict.get("code") == '200':
                login_user(user_dict.get("data"))
        return result_dict
